home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group92c.txt / 000032_icon-group-sender _Thu Oct 22 06:18:10 1992.msg < prev    next >
Internet Message Format  |  1993-01-04  |  3KB

  1. Received: by cheltenham.cs.arizona.edu; Thu, 22 Oct 1992 06:00:08 MST
  2. Date: Thu, 22 Oct 92 06:18:10 -0400
  3. From: isidev!nowlin@uunet.uu.net
  4. Message-Id: <9210221018.AA22763@relay1.UU.NET>
  5. To: uunet!cs.arizona.edu!icon-group@uunet.UU.NET
  6. Subject: Re: skipping comments
  7. Status: R
  8. Errors-To: icon-group-errors@cs.arizona.edu
  9.  
  10.  > From: uunet!arizona!CU.NIH.GOV!UBY
  11.  > Date:     Tue, 13 Oct 1992  11:31:31 EDT
  12.  > Subject:  How to skip comments?
  13.  >
  14.  > I have been trying to modify the concord.icn program from the Icon program
  15.  > library to produce cross-reference tables for SAS programs, but I am
  16.  > stumped about how to skip over comments.  Comments in the SAS language are
  17.  > of the form /* ..... */.  I have not been able to figure out how to skip
  18.  > the comments and still persuade A*B to parse as A and B. The relevent code
  19.  > is appended.  Any suggestions would be greatly appreciated.
  20.  >
  21.  > Thanks,
  22.  >
  23.  > Jim Blakley    UBY@NIHCU.BITNET
  24.  >
  25.  > An Icon novice in over his depth
  26.  
  27. I took the procedure you posted and extrapolated a main() procedure to test
  28. it with.  Including a main() is a good idea when posting code.  Sometimes
  29. no main() can obscure the meaning of code fragments.  I assumed lineno and
  30. keywords were supposed to be global and just threw in some C keywords for
  31. testing.
  32.  
  33. Then I added a new procedure called stripcom() that does what you want.
  34. It's been over eight years since I did any SAS but C uses the same
  35. commenting convention so I had plenty of code to test this on.  See if this
  36. is what you had in mind.
  37.  
  38. This could probably be done in a more iconic fashion but that would just
  39. make the code harder to follow and you said you're a novice.  If you have
  40. questions let me know.  I tried mailing directly but had no luck.
  41.  
  42. Jerry Nowlin
  43. uunet!isidev!nowlin
  44. isidev!nowlin@uunet.uu.net
  45.  
  46. --------------------------------> cut here <-------------------------------
  47.  
  48. ##### ORIGINAL CODE
  49.  
  50. procedure item()
  51.    local i, word, line
  52.    static char1, sasvar, sas
  53.  
  54.    initial {
  55.       char1 := '&%_' ++ &letters
  56.       sasvar := char1 ++ &digits
  57.       }
  58.  
  59.    while line := read() do {
  60.       lineno +:= 1
  61.       write(right(lineno, 6), "  ", line)
  62.       line := map(stripcom(line))                    # fold to lowercase
  63.       i := 1
  64.       line ? {
  65.          while tab(upto(sasvar)) do {
  66.             word := tab(many(sasvar))
  67.             if member(keywords, word) then next      # skip reserved words
  68.             if word ? any(char1) then suspend word   # skip numbers
  69.             }
  70.          }
  71.       }
  72. end
  73.  
  74. ##### NEW CODE
  75.  
  76. procedure stripcom(old)
  77.  
  78.     local    new
  79.     static    incom, begcom, endcom
  80.     initial    { incom := &null ; begcom := "/*" ; endcom := "*/" }
  81.  
  82.     new := ""
  83.  
  84.     old ? while not pos(0) do {
  85.         \incom & ((tab(find(endcom)) & incom := &null) | tab(0))
  86.  
  87.         while new ||:= tab(find(begcom)) do {
  88.             move(2)
  89.             (tab(find(endcom)) & move(2) & incom := &null) |
  90.             (tab(0) & incom := 1)
  91.         }
  92.  
  93.         new ||:= tab(0)
  94.     }
  95.  
  96.     return new
  97. end
  98.  
  99. global lineno, keywords
  100.  
  101. procedure main()
  102.     lineno := 0
  103.     keywords:= set(["break","case","continue","do","else",
  104.             "for","if","return","switch","while"])
  105.     every write(item())
  106. end
  107.  
  108.